package com.zzptc.mzb.myapplication124;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.zzptc.mzb.myapplication124.adapter.QICheGuAdapter;
import com.zzptc.mzb.myapplication124.entity.CarList;
import com.zzptc.mzb.myapplication124.entity.QicheGuzhuang;
import com.zzptc.mzb.myapplication124.service.CarlistService;

import org.litepal.LitePal;
import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    TextView textView;
    ListView lv_qicheguzhang;
    List<QicheGuzhuang>qicheGuzhuangs=new ArrayList<>();
    ArrayAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=findViewById(R.id.textview);
        //getCarlistService();
        getQiCheGuzhangInfo();
        initView();
    }

    private void initView() {
        lv_qicheguzhang=findViewById(R.id.lv_qicheguzhang);
        qicheGuzhuangs= LitePal.findAll(QicheGuzhuang.class);
        adapter=new QICheGuAdapter(getApplicationContext(),R.layout.qicheguzhang,qicheGuzhuangs);
        lv_qicheguzhang.setAdapter(adapter);

    }

    private void getQiCheGuzhangInfo() {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://apis.juhe.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
       CarlistService carlistService=retrofit.create(CarlistService.class);
       Call<QicheGuzhuang> qicheGuzhuangCall=carlistService
               .getQicgeGuzhangAll("6b40135084049a9641f5dc01af7ed188","P2079");
       qicheGuzhuangCall.enqueue(new Callback<QicheGuzhuang>() {
           @Override
           public void onResponse(Call<QicheGuzhuang> call, Response<QicheGuzhuang> response) {
               QicheGuzhuang qi = response.body();
             new QicheGuzhuang().save();
           }

           @Override
           public void onFailure(Call<QicheGuzhuang> call, Throwable t) {
               Log.d(TAG, "onFailure: "+t.getMessage());
           }
       });

    }

    private void getCarlistService() {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://www.hnzzsgz.cn:5001/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CarlistService carlistService=retrofit.create(CarlistService.class);
        Call<CarList> all=carlistService.getAll();
        all.enqueue(new Callback<CarList>() {
            @Override
            public void onResponse(Call<CarList> call, Response<CarList> response) {
                CarList ca=response.body();
                String s=ca.getData().get(0).getCarplate();
                textView.setText("品牌："+s);
                Log.d(TAG, "onResponse: ");
            }

            @Override
            public void onFailure(Call<CarList> call, Throwable t) {
                Log.d(TAG, "onFailure: "+t.getMessage());

            }
        });

    }
}
